package org.leaf.power;

import org.cloudbus.cloudsim.power.PowerMeasurement;
import org.cloudbus.cloudsim.power.models.PowerModel;
import org.leaf.infrastructure.NetworkLink;

/**
 * Linear power model for network links
 * 基于带宽使用量来计算功耗
 */
public class PowerModelNetworkLink implements PowerModel {

    public static PowerModelNetworkLink NULL = new PowerModelNetworkLink(0, 0) {
        @Override public PowerMeasurement getPowerMeasurement() { return new PowerMeasurement(); }
    };

    private double energyPerBit;   // 基础能耗因子
    private double exponentialFactor;   // 用于指数模型的因子
    public PowerModelNetworkLink(final double energyPerBit, final double exponentialFactor) {
        this.exponentialFactor = exponentialFactor;
        this.energyPerBit =energyPerBit;
    }

    private NetworkLink link;

    public NetworkLink getLink() {
        return link;
    }

    public void setLink(NetworkLink link) {
        this.link = link;
    }

    @Override
    public PowerMeasurement getPowerMeasurement() {
//        //P = a * exp(b * Bandwidth)
//        double power = getLink().getUsedBandwidth()*energyPerBit * Math.exp(exponentialFactor * getLink().getUsedBandwidth());
//        return new PowerMeasurement(0, power);
//        //return new PowerMeasurement(0, energyPerBit * getLink().getUsedBandwidth());
        double usedBandwidth = getLink().getUsedBandwidth();
        if (usedBandwidth == 0) return new PowerMeasurement();
        double power = getLink().getUsedBandwidth()*energyPerBit * Math.exp(exponentialFactor * getLink().getUsedBandwidth());
        return new PowerMeasurement(0, power);
    }

    public double getEnergyPerBit() {
        return energyPerBit;
    }

}
